﻿using Magicodes.Logger;
using Magicodes.Logger.NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TXH.DataSync.Config;

namespace TXH.DataSync.Helper
{
    public static class ConfigHelper
    {
        public static string Path = string.Format("{0}//Config.xml", Environment.CurrentDirectory);

        public static LoggerBase Logger = new NLogLogger("ConfigHelper");
        public static ConfigInfo ConfigInfo { get; set; }
        public static bool Exist()
        {
            return System.IO.File.Exists(Path);
        }

        public static bool Init()
        {
            if (Exist())
            {
                using (var fs = File.OpenRead(Path))
                {
                    ConfigInfo = XmlHelper.DeserializeObject<ConfigInfo>(fs);
                }
                if (string.IsNullOrWhiteSpace(ConfigHelper.ConfigInfo.ConnectionString))
                {
                    Console.WriteLine("连接字符串未配置...");
                    Logger.Log(LoggerLevels.Error, "连接字符串未配置...");
                    return false;
                }

                if (string.IsNullOrWhiteSpace(ConfigHelper.ConfigInfo.RemoteApiUrl))
                {
                    Console.WriteLine("接口地址未配置...");
                    Logger.Log(LoggerLevels.Error, "接口地址未配置...");
                    return false;
                }

                if (ConfigHelper.ConfigInfo.TableReaders == null || ConfigHelper.ConfigInfo.TableReaders.Count == 0)
                {
                    Console.WriteLine("没有找到TableReaders的相关配置...");
                    Logger.Log(LoggerLevels.Error, "没有找到TableReaders的相关配置...");
                    return false;
                }

                return true;
            }
            else
            {
                Console.WriteLine("配置文件不存在！Path:" + Path);
                Logger.Log(LoggerLevels.Error, "配置文件不存在！Path:" + Path);
            }
            return false;
        }
    }
}
